{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'paddlex'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-bc1fa897ccf3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpaddlex\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpdx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpaddlex\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtransforms\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),transforms.RandomHorizontalFlip(),\n\u001b[0;32m      4\u001b[0m                                     transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,\n\u001b[0;32m      5\u001b[0m                                                            \u001b[0mcontrast_range\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcontrast_prob\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'paddlex'"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "from paddlex import transforms\n",
    "train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),transforms.RandomHorizontalFlip(),\n",
    "                                    transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,\n",
    "                                                           contrast_range=0.9,contrast_prob=0.5,\n",
    "                                                           saturation_range=0.9,saturation_prob=0.5,\n",
    "                                                           hue_range=18,hue_prob=0.5),\n",
    "                                    transforms.Normalize()])\n",
    "val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n",
    "                                  transforms.CenterCrop(crop_size=224),\n",
    "                                  transforms.Normalize()])\n",
    "train_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='garbage',\n",
    "    file_list='./train.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=train_transforms,\n",
    "    shuffle=True\n",
    ")\n",
    "val_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='garbage',\n",
    "    file_list='./val.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=val_transforms\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'paddlex'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-19f0ca878068>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpaddlex\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpdx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mnum_classes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtrain_dataset\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mmodel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpdx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcls\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mResNet50_vd_ssld\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum_classes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnum_classes\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'paddlex'"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "num_classes=len(train_dataset.labels)\n",
    "model=pdx.cls.ResNet50_vd_ssld(num_classes=num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'model' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-15ceb1e4ec96>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m model.train(num_epochs=5,\n\u001b[0m\u001b[0;32m      2\u001b[0m            \u001b[0mtrain_dataset\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtrain_dataset\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m            \u001b[0mtrain_batch_size\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m16\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m            \u001b[0meval_dataset\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mval_dataset\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m            \u001b[0mlr_decay_epochs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m80\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m150\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'model' is not defined"
     ]
    }
   ],
   "source": [
    "model.train(num_epochs=5,\n",
    "           train_dataset=train_dataset,\n",
    "           train_batch_size=16,\n",
    "           eval_dataset=val_dataset,\n",
    "           lr_decay_epochs=[80,100,150],\n",
    "           save_interval_epochs=1,\n",
    "           learning_rate=0.002,\n",
    "           save_dir='output/ResNet50_vd_ssld',\n",
    "           use_vdl=True)\n",
    "model=pdx.load_model('output/ResNet50_vd_ssld/best_model')\n",
    "image_name='./data/garbage/paper/paper10.jpg'\n",
    "result=model.predict(image_name)\n",
    "print('Predict Result:',result)\n",
    "number=result[0]['category']\n",
    "number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
